package org.gp953.pay.service.impl;

import org.gp953.basic.exception.BusinessException;
import org.gp953.basic.service.impl.BaseServiceImpl;
import org.gp953.pay.domain.AlipayInfo;
import org.gp953.pay.domain.PayBill;
import org.gp953.pay.mapper.AlipayInfoMapper;
import org.gp953.pay.mapper.PayBillMapper;
import org.gp953.pay.service.IPayBillService;
import org.gp953.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    /**
     * 统一的支付接口
     * @param payBill
     * @return
     */
    @Override
    public String payRequest(PayBill payBill) {
        // 数据校验
        if (payBill==null){
            throw new BusinessException("支付账单数据能不能为空！");
        }
        if (payBill.getShop_id() == null) {
            throw new BusinessException("商家信息数据能不能为空！");
        }

        AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBill.getShop_id());

        //0 余额 1 支付宝 2 微信 3 银联
        switch (payBill.getPayChannel()){
            case 1:// 支付宝支付
                // 为了能独立分离支付模块所以支付相关的操作在支付模块内完成
                payBillMapper.save(payBill);
                return AlipayUtils.pay(alipayInfo,payBill);
            case 2:// 微信支付
                break;
            case 3:// 银联支付
                break;
            default:
        }


        return null;
    }

    @Override
    public PayBill  queryByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
